/*
 * CharacterMetrics
 * 
 * Project: Search Engine
 * 
 * Copy right 2012 by Phan Hoàn Vũ
 * Email hoanvuphan@gmail.com
 */

package hcmus.fit.ml.tr;

import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.File;
import java.io.IOException;

import net.sourceforge.javaocr.ocrPlugins.CharacterExtractor;

/**
 * @author hvp
 * 
 */
@SuppressWarnings("unused")
public class CharacterMetrics {

    
    private int x_box;
    private int y_box;
    private int width;
    private int high;
    private int onpix;
    private int x_bar;
    private int y_bar;
    private int x2bar;
    private int y2bar;
    private int xybar;
    private int x2ybr;
    private int xy2br;
    private int x_ege;
    private int xegvy;
    private int y_ege;
    private int yegvx;

    public static class Pixel {
        public int red;
        public int green;
        public int blue;

        public Pixel(int r, int g, int b) {
            red = r;
            green = g;
            blue = b;
        }
    }

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        File inputImage = new File("D:/temp/tr2/text.png");
        File outputDir = new File("D:/temp/tr2/");
        CharacterExtractor e = new CharacterExtractor();
        e.slice(inputImage, outputDir, 40, 40);
//        List<BufferedImage> rs = e.extract(inputImage, 20, 20);
//        System.out.println(rs.size());
//        extract(rs.get(0));
    }

    public static CharacterMetrics extract(BufferedImage image) throws IOException {
        boolean onTop = false;
        boolean onLeft = false;
        int top = 0;
        int left = 0;
        int right = 0;
        int bottom = 0;
        int[] samples = new int[3];

        Raster r = image.getRaster();
        for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {
                samples = r.getPixel(x, y, samples);
                Pixel newPixel = new Pixel(samples[0], samples[1], samples[2]);
                if (isOn(newPixel)) {
                    if (!onLeft && x > left) {
                        left = x;
                        onLeft = true;
                    }
                    if (x > right) {
                        right = x;
                    }

                    if (!onTop && y > top) {
                        top = y;
                        onTop = true;
                    }
                    if (y > bottom) {
                        bottom = y;
                    }
                }
            }
        }

        System.out.println("Top: " + top);
        System.out.println("Left: " + left);
        System.out.println("Bottom: " + bottom);
        System.out.println("Right: " + right);
        CharacterMetrics rs = new CharacterMetrics();
        rs.x_box = left;
        rs.y_box = top;
        rs.width = right - left + 1;
        rs.high = bottom - top + 1;
        return null;
    }

    public static boolean isOn(Pixel p) {
        // System.out.println(" R G B : " + p.red + " " + p.green + " "
        // +p.blue);
        return p.red != 255 && p.green != 255 && p.blue != 255;
    }
}
